﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADA.Business;
using ADA.Entity;
using System.IO;
using System.Security.AccessControl;

namespace ADA.Presentation
{
    public partial class LoginForm : Form
    {
        bool flag;
        public LoginForm()
        {
            InitializeComponent();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            lblError.Text = "";
            tbxLoginName.Select();

            var perUserAppData = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string filePath = Path.Combine(perUserAppData, Common.MAA_URL_LOG_NAME);
            if (File.Exists(filePath))
            {
                string text = System.IO.File.ReadAllText(filePath);
                tbxBrowse.Text = text;
            }
            else
            {
                tbxBrowse.Text = "";
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            lblError.Text = "";
            validate();
            if (flag == false)
            {
                return;
            }
            BOLAccount bolAcc = new BOLAccount();
            Admin ad;
            string message;
            string name = tbxLoginName.Text.Trim();
            string password = tbxPassword.Text.Trim();
            if (bolAcc.Login(name, password, out ad, out message) == true)
            {
                tbxLoginName.Text = "";
                tbxPassword.Text = "";
                lblError.Text = "";
                this.Hide();
                new MainForm(ad, this).Show();
            }
            else
            {
                lblError.Text = message;
            }
        }

        private void validate()
        {
            flag = true;
            if ((tbxLoginName.Text.Trim() == ""))
            {
                errorName.SetError(tbxLoginName, Common.MESSAGE_ERROR_MUST_INPUT_DATA);
                flag = false;
            }
            else
            {
                errorName.Clear();
            }
            if ((tbxPassword.Text.Trim() == ""))
            {
                errorPass.SetError(tbxPassword, Common.MESSAGE_ERROR_MUST_INPUT_DATA);
                flag = false;
            }
            else
            {
                errorPass.Clear();
            }
            if ((tbxBrowse.Text.Trim() == ""))
            {
                errorBrowse.SetError(tbxBrowse, Common.MESSAGE_ERROR_MUST_INPUT_DATA);
                flag = false;
            }
            else
            {
                string MAApath = tbxBrowse.Text.Trim();
                StringBuilder strbld = new StringBuilder(MAApath);
                strbld = strbld.Replace("\r\n", "");

                if (Path.GetFileName(MAApath).Equals("RTF.MAA") == false)
                {
                    errorBrowse.SetError(tbxBrowse, Common.MESSAGE_ERROR_DIRECTORY_INVALID);
                    flag = false;
                }
                else
                {
                    errorPass.Clear();
                    var perUserAppData = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    string filePath = Path.Combine(perUserAppData, Common.MAA_URL_LOG_NAME);
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(filePath, false))
                    {
                        file.WriteLine(MAApath);
                    }
                    Common.URL_MAA = MAApath;
                    Common.URL_RICE = Common.URL_MAA + Common.URL_RICE;
                    Common.URL_NEWS = Common.URL_MAA + Common.URL_NEWS;
                }
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                tbxBrowse.Text = folderBrowserDialog.SelectedPath;
            }
        }
    }
}
